{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# pandas中最常用的两个库\r\n",
    "series\r\n",
    "DataFrame\r\n",
    "## series是一种类似与一维数组的对象，由以下两个部分组成：values一组数据（ndarray类型），index相关额数据索引标签。series的创建是由列表或者numpy数组或者字典创建\r\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "import pandas as pd"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "#指定显示索引 默认是隐式的 只能是一维的data\r\n",
    "a = pd.Series(data=[1,2,2],index=['A','B','C'])"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "# series的索引切片和一维数组一样\r\n",
    "a[0:2]\r\n",
    "a['A':'B']"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "A    1\n",
       "B    2\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "a.shape\r\n",
    "a.size"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "a.head(2)\r\n",
    "a.tail(2)\r\n",
    "# 显示前后的两个元素"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "B    2\n",
       "C    2\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "#数组去重\r\n",
    "a.unique()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([1, 2], dtype=int64)"
      ]
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "# 清洗空值\r\n",
    "wash_none = pd.Series([1,2,3,None,5,None])\r\n",
    "wash_none"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0    1.0\n",
       "1    2.0\n",
       "2    3.0\n",
       "3    NaN\n",
       "4    5.0\n",
       "5    NaN\n",
       "dtype: float64"
      ]
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "wash_none.notnull()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0     True\n",
       "1     True\n",
       "2     True\n",
       "3    False\n",
       "4     True\n",
       "5    False\n",
       "dtype: bool"
      ]
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "source": [
    "wash_none[wash_none.notnull()]"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0    1.0\n",
       "1    2.0\n",
       "2    3.0\n",
       "4    5.0\n",
       "dtype: float64"
      ]
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# DateFrame ：是一个表格型的数据结构，index,colums,values,行索引列索引值\r\n",
    "数据源可以是numpy数组或者自定义数组\r\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "source": [
    "import numpy as np\r\n",
    "data_frame_arr = pd.DataFrame(data=np.random.randint(90,100,size=(5,3)) ,index=['张三','李四','王五','赵六','哈哈'],columns=['语文','数学','英语'],)\r\n",
    "#字典的key默认作为colums\r\n",
    "data_frame_arr"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>94</td>\n",
       "      <td>96</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>99</td>\n",
       "      <td>93</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>97</td>\n",
       "      <td>96</td>\n",
       "      <td>97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>94</td>\n",
       "      <td>92</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>哈哈</th>\n",
       "      <td>94</td>\n",
       "      <td>91</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语\n",
       "张三  94  96  95\n",
       "李四  99  93  91\n",
       "王五  97  96  97\n",
       "赵六  94  92  98\n",
       "哈哈  94  91  96"
      ]
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "source": [
    "#取列\r\n",
    "data_frame_arr\r\n",
    "data_frame_arr['数学']"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "张三    96\n",
       "李四    93\n",
       "王五    96\n",
       "赵六    92\n",
       "哈哈    91\n",
       "Name: 数学, dtype: int32"
      ]
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "source": [
    "#切片\r\n",
    "frame_slice = pd.DataFrame(np.random.randint(0,100,size=(3,6)))\r\n",
    "frame_slice"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>94</td>\n",
       "      <td>97</td>\n",
       "      <td>35</td>\n",
       "      <td>61</td>\n",
       "      <td>44</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>31</td>\n",
       "      <td>50</td>\n",
       "      <td>10</td>\n",
       "      <td>30</td>\n",
       "      <td>92</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>67</td>\n",
       "      <td>88</td>\n",
       "      <td>95</td>\n",
       "      <td>90</td>\n",
       "      <td>25</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1   2   3   4   5\n",
       "0  94  97  35  61  44  91\n",
       "1  31  50  10  30  92  67\n",
       "2  67  88  95  90  25  24"
      ]
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "source": [
    "# 和数组的切法是一样的\r\n",
    "frame_slice.iloc[0:2,0:2]"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>94</td>\n",
       "      <td>97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>31</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1\n",
       "0  94  97\n",
       "1  31  50"
      ]
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 需求： 股票分析\r\n",
    "· 使用tushare库获取股票行情的历史数据\r\n",
    "\r\n",
    "· 输出该股票的所有收盘比开盘上涨3%以上的日期\r\n",
    "\r\n",
    "· 输出该股票的所有收盘比前日开盘跌幅2%以上的日期\r\n",
    "\r\n",
    "· 假如从10年1.1开始，每月第一个交易日买入一手股票，每年最后一个交易日卖出所有，到今为止，收益如何？"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "source": [
    "import tushare as ts\r\n",
    "import numpy as np"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "source": [
    "df = ts.get_k_data(code='600519',start='1991-01-01')\r\n",
    "type (df)\r\n",
    "df.to_csv('./maotai.csv')"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "本接口即将停止更新，请尽快使用Pro版接口：https://waditu.com/document/2\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "source": [
    "df = pd.read_csv('./maotai.csv')\r\n",
    "df"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>date</th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2001-08-27</td>\n",
       "      <td>5.392</td>\n",
       "      <td>5.554</td>\n",
       "      <td>5.902</td>\n",
       "      <td>5.132</td>\n",
       "      <td>406318.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2001-08-28</td>\n",
       "      <td>5.467</td>\n",
       "      <td>5.759</td>\n",
       "      <td>5.781</td>\n",
       "      <td>5.407</td>\n",
       "      <td>129647.79</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2001-08-29</td>\n",
       "      <td>5.777</td>\n",
       "      <td>5.684</td>\n",
       "      <td>5.781</td>\n",
       "      <td>5.640</td>\n",
       "      <td>53252.75</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>2001-08-30</td>\n",
       "      <td>5.668</td>\n",
       "      <td>5.796</td>\n",
       "      <td>5.860</td>\n",
       "      <td>5.624</td>\n",
       "      <td>48013.06</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>2001-08-31</td>\n",
       "      <td>5.804</td>\n",
       "      <td>5.782</td>\n",
       "      <td>5.877</td>\n",
       "      <td>5.749</td>\n",
       "      <td>23231.48</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4781</th>\n",
       "      <td>4781</td>\n",
       "      <td>2021-08-30</td>\n",
       "      <td>1605.000</td>\n",
       "      <td>1586.000</td>\n",
       "      <td>1613.000</td>\n",
       "      <td>1545.950</td>\n",
       "      <td>51588.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4782</th>\n",
       "      <td>4782</td>\n",
       "      <td>2021-08-31</td>\n",
       "      <td>1589.800</td>\n",
       "      <td>1558.000</td>\n",
       "      <td>1616.350</td>\n",
       "      <td>1555.100</td>\n",
       "      <td>43677.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4783</th>\n",
       "      <td>4783</td>\n",
       "      <td>2021-09-01</td>\n",
       "      <td>1559.000</td>\n",
       "      <td>1622.010</td>\n",
       "      <td>1634.990</td>\n",
       "      <td>1531.100</td>\n",
       "      <td>77316.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4784</th>\n",
       "      <td>4784</td>\n",
       "      <td>2021-09-02</td>\n",
       "      <td>1632.990</td>\n",
       "      <td>1618.800</td>\n",
       "      <td>1643.000</td>\n",
       "      <td>1600.890</td>\n",
       "      <td>42837.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4785</th>\n",
       "      <td>4785</td>\n",
       "      <td>2021-09-03</td>\n",
       "      <td>1610.020</td>\n",
       "      <td>1658.220</td>\n",
       "      <td>1659.790</td>\n",
       "      <td>1582.000</td>\n",
       "      <td>52835.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4786 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Unnamed: 0        date      open     close      high       low  \\\n",
       "0              0  2001-08-27     5.392     5.554     5.902     5.132   \n",
       "1              1  2001-08-28     5.467     5.759     5.781     5.407   \n",
       "2              2  2001-08-29     5.777     5.684     5.781     5.640   \n",
       "3              3  2001-08-30     5.668     5.796     5.860     5.624   \n",
       "4              4  2001-08-31     5.804     5.782     5.877     5.749   \n",
       "...          ...         ...       ...       ...       ...       ...   \n",
       "4781        4781  2021-08-30  1605.000  1586.000  1613.000  1545.950   \n",
       "4782        4782  2021-08-31  1589.800  1558.000  1616.350  1555.100   \n",
       "4783        4783  2021-09-01  1559.000  1622.010  1634.990  1531.100   \n",
       "4784        4784  2021-09-02  1632.990  1618.800  1643.000  1600.890   \n",
       "4785        4785  2021-09-03  1610.020  1658.220  1659.790  1582.000   \n",
       "\n",
       "         volume    code  \n",
       "0     406318.00  600519  \n",
       "1     129647.79  600519  \n",
       "2      53252.75  600519  \n",
       "3      48013.06  600519  \n",
       "4      23231.48  600519  \n",
       "...         ...     ...  \n",
       "4781   51588.00  600519  \n",
       "4782   43677.00  600519  \n",
       "4783   77316.00  600519  \n",
       "4784   42837.00  600519  \n",
       "4785   52835.00  600519  \n",
       "\n",
       "[4786 rows x 8 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "source": [
    "df.info()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 4786 entries, 0 to 4785\n",
      "Data columns (total 8 columns):\n",
      " #   Column      Non-Null Count  Dtype  \n",
      "---  ------      --------------  -----  \n",
      " 0   Unnamed: 0  4786 non-null   int64  \n",
      " 1   date        4786 non-null   object \n",
      " 2   open        4786 non-null   float64\n",
      " 3   close       4786 non-null   float64\n",
      " 4   high        4786 non-null   float64\n",
      " 5   low         4786 non-null   float64\n",
      " 6   volume      4786 non-null   float64\n",
      " 7   code        4786 non-null   int64  \n",
      "dtypes: float64(5), int64(2), object(1)\n",
      "memory usage: 280.5+ KB\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "source": [
    "# 将字符串类型的时间转换成时间\r\n",
    "df['date'] = pd.to_datetime(df['date'])\r\n",
    "df.info()\r\n",
    "df"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 4786 entries, 0 to 4785\n",
      "Data columns (total 8 columns):\n",
      " #   Column      Non-Null Count  Dtype         \n",
      "---  ------      --------------  -----         \n",
      " 0   Unnamed: 0  4786 non-null   int64         \n",
      " 1   date        4786 non-null   datetime64[ns]\n",
      " 2   open        4786 non-null   float64       \n",
      " 3   close       4786 non-null   float64       \n",
      " 4   high        4786 non-null   float64       \n",
      " 5   low         4786 non-null   float64       \n",
      " 6   volume      4786 non-null   float64       \n",
      " 7   code        4786 non-null   int64         \n",
      "dtypes: datetime64[ns](1), float64(5), int64(2)\n",
      "memory usage: 299.2 KB\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>date</th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2001-08-27</td>\n",
       "      <td>5.392</td>\n",
       "      <td>5.554</td>\n",
       "      <td>5.902</td>\n",
       "      <td>5.132</td>\n",
       "      <td>406318.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2001-08-28</td>\n",
       "      <td>5.467</td>\n",
       "      <td>5.759</td>\n",
       "      <td>5.781</td>\n",
       "      <td>5.407</td>\n",
       "      <td>129647.79</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2001-08-29</td>\n",
       "      <td>5.777</td>\n",
       "      <td>5.684</td>\n",
       "      <td>5.781</td>\n",
       "      <td>5.640</td>\n",
       "      <td>53252.75</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>2001-08-30</td>\n",
       "      <td>5.668</td>\n",
       "      <td>5.796</td>\n",
       "      <td>5.860</td>\n",
       "      <td>5.624</td>\n",
       "      <td>48013.06</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>2001-08-31</td>\n",
       "      <td>5.804</td>\n",
       "      <td>5.782</td>\n",
       "      <td>5.877</td>\n",
       "      <td>5.749</td>\n",
       "      <td>23231.48</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4781</th>\n",
       "      <td>4781</td>\n",
       "      <td>2021-08-30</td>\n",
       "      <td>1605.000</td>\n",
       "      <td>1586.000</td>\n",
       "      <td>1613.000</td>\n",
       "      <td>1545.950</td>\n",
       "      <td>51588.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4782</th>\n",
       "      <td>4782</td>\n",
       "      <td>2021-08-31</td>\n",
       "      <td>1589.800</td>\n",
       "      <td>1558.000</td>\n",
       "      <td>1616.350</td>\n",
       "      <td>1555.100</td>\n",
       "      <td>43677.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4783</th>\n",
       "      <td>4783</td>\n",
       "      <td>2021-09-01</td>\n",
       "      <td>1559.000</td>\n",
       "      <td>1622.010</td>\n",
       "      <td>1634.990</td>\n",
       "      <td>1531.100</td>\n",
       "      <td>77316.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4784</th>\n",
       "      <td>4784</td>\n",
       "      <td>2021-09-02</td>\n",
       "      <td>1632.990</td>\n",
       "      <td>1618.800</td>\n",
       "      <td>1643.000</td>\n",
       "      <td>1600.890</td>\n",
       "      <td>42837.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4785</th>\n",
       "      <td>4785</td>\n",
       "      <td>2021-09-03</td>\n",
       "      <td>1610.020</td>\n",
       "      <td>1658.220</td>\n",
       "      <td>1659.790</td>\n",
       "      <td>1582.000</td>\n",
       "      <td>52835.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4786 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Unnamed: 0       date      open     close      high       low  \\\n",
       "0              0 2001-08-27     5.392     5.554     5.902     5.132   \n",
       "1              1 2001-08-28     5.467     5.759     5.781     5.407   \n",
       "2              2 2001-08-29     5.777     5.684     5.781     5.640   \n",
       "3              3 2001-08-30     5.668     5.796     5.860     5.624   \n",
       "4              4 2001-08-31     5.804     5.782     5.877     5.749   \n",
       "...          ...        ...       ...       ...       ...       ...   \n",
       "4781        4781 2021-08-30  1605.000  1586.000  1613.000  1545.950   \n",
       "4782        4782 2021-08-31  1589.800  1558.000  1616.350  1555.100   \n",
       "4783        4783 2021-09-01  1559.000  1622.010  1634.990  1531.100   \n",
       "4784        4784 2021-09-02  1632.990  1618.800  1643.000  1600.890   \n",
       "4785        4785 2021-09-03  1610.020  1658.220  1659.790  1582.000   \n",
       "\n",
       "         volume    code  \n",
       "0     406318.00  600519  \n",
       "1     129647.79  600519  \n",
       "2      53252.75  600519  \n",
       "3      48013.06  600519  \n",
       "4      23231.48  600519  \n",
       "...         ...     ...  \n",
       "4781   51588.00  600519  \n",
       "4782   43677.00  600519  \n",
       "4783   77316.00  600519  \n",
       "4784   42837.00  600519  \n",
       "4785   52835.00  600519  \n",
       "\n",
       "[4786 rows x 8 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 19
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "source": [
    "#将指定列的数据作为表格数据的行索引\r\n",
    "df.set_index('date',inplace=True)\r\n",
    "# 删除指定一列的数据"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "source": [
    "df"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2001-08-27</th>\n",
       "      <td>0</td>\n",
       "      <td>5.392</td>\n",
       "      <td>5.554</td>\n",
       "      <td>5.902</td>\n",
       "      <td>5.132</td>\n",
       "      <td>406318.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-28</th>\n",
       "      <td>1</td>\n",
       "      <td>5.467</td>\n",
       "      <td>5.759</td>\n",
       "      <td>5.781</td>\n",
       "      <td>5.407</td>\n",
       "      <td>129647.79</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-29</th>\n",
       "      <td>2</td>\n",
       "      <td>5.777</td>\n",
       "      <td>5.684</td>\n",
       "      <td>5.781</td>\n",
       "      <td>5.640</td>\n",
       "      <td>53252.75</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-30</th>\n",
       "      <td>3</td>\n",
       "      <td>5.668</td>\n",
       "      <td>5.796</td>\n",
       "      <td>5.860</td>\n",
       "      <td>5.624</td>\n",
       "      <td>48013.06</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-31</th>\n",
       "      <td>4</td>\n",
       "      <td>5.804</td>\n",
       "      <td>5.782</td>\n",
       "      <td>5.877</td>\n",
       "      <td>5.749</td>\n",
       "      <td>23231.48</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-30</th>\n",
       "      <td>4781</td>\n",
       "      <td>1605.000</td>\n",
       "      <td>1586.000</td>\n",
       "      <td>1613.000</td>\n",
       "      <td>1545.950</td>\n",
       "      <td>51588.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-31</th>\n",
       "      <td>4782</td>\n",
       "      <td>1589.800</td>\n",
       "      <td>1558.000</td>\n",
       "      <td>1616.350</td>\n",
       "      <td>1555.100</td>\n",
       "      <td>43677.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-01</th>\n",
       "      <td>4783</td>\n",
       "      <td>1559.000</td>\n",
       "      <td>1622.010</td>\n",
       "      <td>1634.990</td>\n",
       "      <td>1531.100</td>\n",
       "      <td>77316.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-02</th>\n",
       "      <td>4784</td>\n",
       "      <td>1632.990</td>\n",
       "      <td>1618.800</td>\n",
       "      <td>1643.000</td>\n",
       "      <td>1600.890</td>\n",
       "      <td>42837.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-03</th>\n",
       "      <td>4785</td>\n",
       "      <td>1610.020</td>\n",
       "      <td>1658.220</td>\n",
       "      <td>1659.790</td>\n",
       "      <td>1582.000</td>\n",
       "      <td>52835.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4786 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Unnamed: 0      open     close      high       low     volume  \\\n",
       "date                                                                        \n",
       "2001-08-27           0     5.392     5.554     5.902     5.132  406318.00   \n",
       "2001-08-28           1     5.467     5.759     5.781     5.407  129647.79   \n",
       "2001-08-29           2     5.777     5.684     5.781     5.640   53252.75   \n",
       "2001-08-30           3     5.668     5.796     5.860     5.624   48013.06   \n",
       "2001-08-31           4     5.804     5.782     5.877     5.749   23231.48   \n",
       "...                ...       ...       ...       ...       ...        ...   \n",
       "2021-08-30        4781  1605.000  1586.000  1613.000  1545.950   51588.00   \n",
       "2021-08-31        4782  1589.800  1558.000  1616.350  1555.100   43677.00   \n",
       "2021-09-01        4783  1559.000  1622.010  1634.990  1531.100   77316.00   \n",
       "2021-09-02        4784  1632.990  1618.800  1643.000  1600.890   42837.00   \n",
       "2021-09-03        4785  1610.020  1658.220  1659.790  1582.000   52835.00   \n",
       "\n",
       "              code  \n",
       "date                \n",
       "2001-08-27  600519  \n",
       "2001-08-28  600519  \n",
       "2001-08-29  600519  \n",
       "2001-08-30  600519  \n",
       "2001-08-31  600519  \n",
       "...            ...  \n",
       "2021-08-30  600519  \n",
       "2021-08-31  600519  \n",
       "2021-09-01  600519  \n",
       "2021-09-02  600519  \n",
       "2021-09-03  600519  \n",
       "\n",
       "[4786 rows x 7 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 21
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "source": [
    "df.drop(labels='Unnamed: 0',axis=1,inplace=True)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "source": [
    "df"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2001-08-27</th>\n",
       "      <td>5.392</td>\n",
       "      <td>5.554</td>\n",
       "      <td>5.902</td>\n",
       "      <td>5.132</td>\n",
       "      <td>406318.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-28</th>\n",
       "      <td>5.467</td>\n",
       "      <td>5.759</td>\n",
       "      <td>5.781</td>\n",
       "      <td>5.407</td>\n",
       "      <td>129647.79</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-29</th>\n",
       "      <td>5.777</td>\n",
       "      <td>5.684</td>\n",
       "      <td>5.781</td>\n",
       "      <td>5.640</td>\n",
       "      <td>53252.75</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-30</th>\n",
       "      <td>5.668</td>\n",
       "      <td>5.796</td>\n",
       "      <td>5.860</td>\n",
       "      <td>5.624</td>\n",
       "      <td>48013.06</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-31</th>\n",
       "      <td>5.804</td>\n",
       "      <td>5.782</td>\n",
       "      <td>5.877</td>\n",
       "      <td>5.749</td>\n",
       "      <td>23231.48</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-30</th>\n",
       "      <td>1605.000</td>\n",
       "      <td>1586.000</td>\n",
       "      <td>1613.000</td>\n",
       "      <td>1545.950</td>\n",
       "      <td>51588.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-31</th>\n",
       "      <td>1589.800</td>\n",
       "      <td>1558.000</td>\n",
       "      <td>1616.350</td>\n",
       "      <td>1555.100</td>\n",
       "      <td>43677.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-01</th>\n",
       "      <td>1559.000</td>\n",
       "      <td>1622.010</td>\n",
       "      <td>1634.990</td>\n",
       "      <td>1531.100</td>\n",
       "      <td>77316.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-02</th>\n",
       "      <td>1632.990</td>\n",
       "      <td>1618.800</td>\n",
       "      <td>1643.000</td>\n",
       "      <td>1600.890</td>\n",
       "      <td>42837.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-03</th>\n",
       "      <td>1610.020</td>\n",
       "      <td>1658.220</td>\n",
       "      <td>1659.790</td>\n",
       "      <td>1582.000</td>\n",
       "      <td>52835.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4786 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                open     close      high       low     volume    code\n",
       "date                                                                 \n",
       "2001-08-27     5.392     5.554     5.902     5.132  406318.00  600519\n",
       "2001-08-28     5.467     5.759     5.781     5.407  129647.79  600519\n",
       "2001-08-29     5.777     5.684     5.781     5.640   53252.75  600519\n",
       "2001-08-30     5.668     5.796     5.860     5.624   48013.06  600519\n",
       "2001-08-31     5.804     5.782     5.877     5.749   23231.48  600519\n",
       "...              ...       ...       ...       ...        ...     ...\n",
       "2021-08-30  1605.000  1586.000  1613.000  1545.950   51588.00  600519\n",
       "2021-08-31  1589.800  1558.000  1616.350  1555.100   43677.00  600519\n",
       "2021-09-01  1559.000  1622.010  1634.990  1531.100   77316.00  600519\n",
       "2021-09-02  1632.990  1618.800  1643.000  1600.890   42837.00  600519\n",
       "2021-09-03  1610.020  1658.220  1659.790  1582.000   52835.00  600519\n",
       "\n",
       "[4786 rows x 6 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "source": [
    "# (close - open) /open > 0.03"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "source": [
    "(df[\"close\"] - df[\"open\"])/df['open'] > 0.03"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "date\n",
       "2001-08-27     True\n",
       "2001-08-28     True\n",
       "2001-08-29    False\n",
       "2001-08-30    False\n",
       "2001-08-31    False\n",
       "              ...  \n",
       "2021-08-30    False\n",
       "2021-08-31    False\n",
       "2021-09-01     True\n",
       "2021-09-02    False\n",
       "2021-09-03    False\n",
       "Length: 4786, dtype: bool"
      ]
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "source": [
    "# 需求一，收盘比开盘高出3%\r\n",
    "# · 输出该股票的所有收盘比开盘上涨3%以上的日期\r\n",
    "\r\n",
    "df[(df[\"close\"] - df[\"open\"])/df['open'] > 0.03]"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2001-08-27</th>\n",
       "      <td>5.392</td>\n",
       "      <td>5.554</td>\n",
       "      <td>5.902</td>\n",
       "      <td>5.132</td>\n",
       "      <td>406318.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-28</th>\n",
       "      <td>5.467</td>\n",
       "      <td>5.759</td>\n",
       "      <td>5.781</td>\n",
       "      <td>5.407</td>\n",
       "      <td>129647.79</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-10</th>\n",
       "      <td>5.531</td>\n",
       "      <td>5.734</td>\n",
       "      <td>5.757</td>\n",
       "      <td>5.470</td>\n",
       "      <td>18878.89</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-12-21</th>\n",
       "      <td>5.421</td>\n",
       "      <td>5.604</td>\n",
       "      <td>5.620</td>\n",
       "      <td>5.421</td>\n",
       "      <td>8135.04</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-18</th>\n",
       "      <td>5.437</td>\n",
       "      <td>5.726</td>\n",
       "      <td>5.762</td>\n",
       "      <td>5.421</td>\n",
       "      <td>32262.08</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-07-28</th>\n",
       "      <td>1703.000</td>\n",
       "      <td>1768.900</td>\n",
       "      <td>1788.200</td>\n",
       "      <td>1682.120</td>\n",
       "      <td>85369.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-02</th>\n",
       "      <td>1664.000</td>\n",
       "      <td>1755.000</td>\n",
       "      <td>1755.980</td>\n",
       "      <td>1620.720</td>\n",
       "      <td>97401.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-10</th>\n",
       "      <td>1700.000</td>\n",
       "      <td>1799.000</td>\n",
       "      <td>1802.880</td>\n",
       "      <td>1675.000</td>\n",
       "      <td>83285.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-24</th>\n",
       "      <td>1575.000</td>\n",
       "      <td>1625.180</td>\n",
       "      <td>1642.550</td>\n",
       "      <td>1565.770</td>\n",
       "      <td>72594.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-01</th>\n",
       "      <td>1559.000</td>\n",
       "      <td>1622.010</td>\n",
       "      <td>1634.990</td>\n",
       "      <td>1531.100</td>\n",
       "      <td>77316.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>333 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                open     close      high       low     volume    code\n",
       "date                                                                 \n",
       "2001-08-27     5.392     5.554     5.902     5.132  406318.00  600519\n",
       "2001-08-28     5.467     5.759     5.781     5.407  129647.79  600519\n",
       "2001-09-10     5.531     5.734     5.757     5.470   18878.89  600519\n",
       "2001-12-21     5.421     5.604     5.620     5.421    8135.04  600519\n",
       "2002-01-18     5.437     5.726     5.762     5.421   32262.08  600519\n",
       "...              ...       ...       ...       ...        ...     ...\n",
       "2021-07-28  1703.000  1768.900  1788.200  1682.120   85369.00  600519\n",
       "2021-08-02  1664.000  1755.000  1755.980  1620.720   97401.00  600519\n",
       "2021-08-10  1700.000  1799.000  1802.880  1675.000   83285.00  600519\n",
       "2021-08-24  1575.000  1625.180  1642.550  1565.770   72594.00  600519\n",
       "2021-09-01  1559.000  1622.010  1634.990  1531.100   77316.00  600519\n",
       "\n",
       "[333 rows x 6 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 26
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "source": [
    "test = pd.DataFrame(np.random.randint(0,100,size=(3,5)))\r\n",
    "test"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>93</td>\n",
       "      <td>83</td>\n",
       "      <td>49</td>\n",
       "      <td>82</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34</td>\n",
       "      <td>81</td>\n",
       "      <td>30</td>\n",
       "      <td>5</td>\n",
       "      <td>56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>37</td>\n",
       "      <td>84</td>\n",
       "      <td>15</td>\n",
       "      <td>69</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1   2   3   4\n",
       "0  93  83  49  82  96\n",
       "1  34  81  30   5  56\n",
       "2  37  84  15  69  70"
      ]
     },
     "metadata": {},
     "execution_count": 27
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "source": [
    "a = test[1]>20\r\n",
    "isshow = pd.core.series.Series([np.bool_(True),np.bool_(False),np.bool_(True)])\r\n",
    "type(a[1])\r\n",
    "type(np.bool_(True))\r\n",
    "type(pd.core.series.Series([np.bool_(True),np.bool_(False),np.bool_(True)]))\r\n",
    "test[isshow]\r\n",
    "#test[test['1']>20]"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>93</td>\n",
       "      <td>83</td>\n",
       "      <td>49</td>\n",
       "      <td>82</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>37</td>\n",
       "      <td>84</td>\n",
       "      <td>15</td>\n",
       "      <td>69</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1   2   3   4\n",
       "0  93  83  49  82  96\n",
       "2  37  84  15  69  70"
      ]
     },
     "metadata": {},
     "execution_count": 28
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "source": [
    "type(a)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "metadata": {},
     "execution_count": 29
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "source": [
    "df"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2001-08-27</th>\n",
       "      <td>5.392</td>\n",
       "      <td>5.554</td>\n",
       "      <td>5.902</td>\n",
       "      <td>5.132</td>\n",
       "      <td>406318.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-28</th>\n",
       "      <td>5.467</td>\n",
       "      <td>5.759</td>\n",
       "      <td>5.781</td>\n",
       "      <td>5.407</td>\n",
       "      <td>129647.79</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-29</th>\n",
       "      <td>5.777</td>\n",
       "      <td>5.684</td>\n",
       "      <td>5.781</td>\n",
       "      <td>5.640</td>\n",
       "      <td>53252.75</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-30</th>\n",
       "      <td>5.668</td>\n",
       "      <td>5.796</td>\n",
       "      <td>5.860</td>\n",
       "      <td>5.624</td>\n",
       "      <td>48013.06</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-31</th>\n",
       "      <td>5.804</td>\n",
       "      <td>5.782</td>\n",
       "      <td>5.877</td>\n",
       "      <td>5.749</td>\n",
       "      <td>23231.48</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-30</th>\n",
       "      <td>1605.000</td>\n",
       "      <td>1586.000</td>\n",
       "      <td>1613.000</td>\n",
       "      <td>1545.950</td>\n",
       "      <td>51588.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-31</th>\n",
       "      <td>1589.800</td>\n",
       "      <td>1558.000</td>\n",
       "      <td>1616.350</td>\n",
       "      <td>1555.100</td>\n",
       "      <td>43677.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-01</th>\n",
       "      <td>1559.000</td>\n",
       "      <td>1622.010</td>\n",
       "      <td>1634.990</td>\n",
       "      <td>1531.100</td>\n",
       "      <td>77316.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-02</th>\n",
       "      <td>1632.990</td>\n",
       "      <td>1618.800</td>\n",
       "      <td>1643.000</td>\n",
       "      <td>1600.890</td>\n",
       "      <td>42837.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-03</th>\n",
       "      <td>1610.020</td>\n",
       "      <td>1658.220</td>\n",
       "      <td>1659.790</td>\n",
       "      <td>1582.000</td>\n",
       "      <td>52835.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4786 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                open     close      high       low     volume    code\n",
       "date                                                                 \n",
       "2001-08-27     5.392     5.554     5.902     5.132  406318.00  600519\n",
       "2001-08-28     5.467     5.759     5.781     5.407  129647.79  600519\n",
       "2001-08-29     5.777     5.684     5.781     5.640   53252.75  600519\n",
       "2001-08-30     5.668     5.796     5.860     5.624   48013.06  600519\n",
       "2001-08-31     5.804     5.782     5.877     5.749   23231.48  600519\n",
       "...              ...       ...       ...       ...        ...     ...\n",
       "2021-08-30  1605.000  1586.000  1613.000  1545.950   51588.00  600519\n",
       "2021-08-31  1589.800  1558.000  1616.350  1555.100   43677.00  600519\n",
       "2021-09-01  1559.000  1622.010  1634.990  1531.100   77316.00  600519\n",
       "2021-09-02  1632.990  1618.800  1643.000  1600.890   42837.00  600519\n",
       "2021-09-03  1610.020  1658.220  1659.790  1582.000   52835.00  600519\n",
       "\n",
       "[4786 rows x 6 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 30
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "source": [
    "ceshi = [[1,2,3],[4,5,6]]\r\n",
    "tt = pd.DataFrame(np.array(ceshi))\r\n",
    "\r\n",
    "tt.size/(tt.size/tt.ndim)\r\n"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "source": [
    "# 输出该股票的所有收盘比前日开盘跌幅2%以上的日期 将前一列数据整体下移，然后做差\r\n",
    "# df[df['close'] - df['open']]\r\n",
    "df[df['close']-df['open'].shift(1) < -0.02]"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2001-09-03</th>\n",
       "      <td>5.812</td>\n",
       "      <td>5.779</td>\n",
       "      <td>5.870</td>\n",
       "      <td>5.757</td>\n",
       "      <td>22112.09</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-06</th>\n",
       "      <td>5.835</td>\n",
       "      <td>5.734</td>\n",
       "      <td>5.854</td>\n",
       "      <td>5.704</td>\n",
       "      <td>28997.03</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-07</th>\n",
       "      <td>5.702</td>\n",
       "      <td>5.574</td>\n",
       "      <td>5.773</td>\n",
       "      <td>5.570</td>\n",
       "      <td>31552.25</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-12</th>\n",
       "      <td>5.520</td>\n",
       "      <td>5.621</td>\n",
       "      <td>5.656</td>\n",
       "      <td>5.515</td>\n",
       "      <td>25045.19</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-17</th>\n",
       "      <td>5.637</td>\n",
       "      <td>5.599</td>\n",
       "      <td>5.670</td>\n",
       "      <td>5.546</td>\n",
       "      <td>8983.97</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-20</th>\n",
       "      <td>1555.170</td>\n",
       "      <td>1548.000</td>\n",
       "      <td>1597.550</td>\n",
       "      <td>1525.500</td>\n",
       "      <td>114545.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-26</th>\n",
       "      <td>1664.990</td>\n",
       "      <td>1595.000</td>\n",
       "      <td>1664.990</td>\n",
       "      <td>1594.720</td>\n",
       "      <td>54020.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-27</th>\n",
       "      <td>1589.000</td>\n",
       "      <td>1596.030</td>\n",
       "      <td>1625.000</td>\n",
       "      <td>1584.100</td>\n",
       "      <td>36095.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-30</th>\n",
       "      <td>1605.000</td>\n",
       "      <td>1586.000</td>\n",
       "      <td>1613.000</td>\n",
       "      <td>1545.950</td>\n",
       "      <td>51588.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-31</th>\n",
       "      <td>1589.800</td>\n",
       "      <td>1558.000</td>\n",
       "      <td>1616.350</td>\n",
       "      <td>1555.100</td>\n",
       "      <td>43677.00</td>\n",
       "      <td>600519</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2176 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                open     close      high       low     volume    code\n",
       "date                                                                 \n",
       "2001-09-03     5.812     5.779     5.870     5.757   22112.09  600519\n",
       "2001-09-06     5.835     5.734     5.854     5.704   28997.03  600519\n",
       "2001-09-07     5.702     5.574     5.773     5.570   31552.25  600519\n",
       "2001-09-12     5.520     5.621     5.656     5.515   25045.19  600519\n",
       "2001-09-17     5.637     5.599     5.670     5.546    8983.97  600519\n",
       "...              ...       ...       ...       ...        ...     ...\n",
       "2021-08-20  1555.170  1548.000  1597.550  1525.500  114545.00  600519\n",
       "2021-08-26  1664.990  1595.000  1664.990  1594.720   54020.00  600519\n",
       "2021-08-27  1589.000  1596.030  1625.000  1584.100   36095.00  600519\n",
       "2021-08-30  1605.000  1586.000  1613.000  1545.950   51588.00  600519\n",
       "2021-08-31  1589.800  1558.000  1616.350  1555.100   43677.00  600519\n",
       "\n",
       "[2176 rows x 6 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 32
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "source": [
    "# 假如从10年1.1开始，每月第一个交易日买入一手股票，每年最后一个交易日卖出所有，到今为止，收益如何？\r\n",
    "# df[:]\r\n",
    "import datetime\r\n",
    "import numpy as np\r\n",
    "import pandas as pd\r\n",
    "# datetime.datetime(2010)\r\n",
    "openMoney = pd.DataFrame([],)\r\n",
    "closeMoney = pd.DataFrame([])\r\n",
    "sum = 0\r\n",
    "\r\n",
    "for year in range(2010,2021):\r\n",
    "    # print(df[str(year)].resample('M').last())\r\n",
    "    #print(df[str(i)].resample('M').last())\r\n",
    "    bug_in = (df[str(year)].resample('M').first().sum())\r\n",
    "    # print(bug_in['open'])\r\n",
    "\r\n",
    "    #print((df[str(i)].resample('M').last()))\r\n",
    "    out = (df[str(year)].resample('M').last()[-1:]).sum()\r\n",
    "    # print(out['open'])\r\n",
    "    # print(((out.iloc[:,[0]]*12)['open']))\r\n",
    "    sum+=(out[\"open\"]*12 - bug_in['open'])\r\n",
    "    \r\n",
    "# print(\"Tet\")\r\n",
    "print(sum)\r\n",
    "   \r\n",
    "    # openMoney.append(df[str(i)].iloc[:1].open)\r\n",
    "    # closeMoney.append(df[str(i)].iloc[:1].close)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "12032.796999999999\n"
     ]
    },
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "C:\\Users\\王飞\\AppData\\Local\\Temp/ipykernel_3192/809920260.py:14: FutureWarning: Indexing a DataFrame with a datetimelike index using a single string to slice the rows, like `frame[string]`, is deprecated and will be removed in a future version. Use `frame.loc[string]` instead.\n",
      "  bug_in = (df[str(year)].resample('M').first().sum())\n",
      "C:\\Users\\王飞\\AppData\\Local\\Temp/ipykernel_3192/809920260.py:18: FutureWarning: Indexing a DataFrame with a datetimelike index using a single string to slice the rows, like `frame[string]`, is deprecated and will be removed in a future version. Use `frame.loc[string]` instead.\n",
      "  out = (df[str(year)].resample('M').last()[-1:]).sum()\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.8.6",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.6 32-bit"
  },
  "interpreter": {
   "hash": "df642eef7b43fe7a4a517bca7a97690968a466268416ac555ac71584a4a4c66c"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}